package com.ljc.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.ljc.dto.Result;
import com.ljc.entity.Administrator;
import com.ljc.service.AdministratorService;
import com.ljc.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.cli.Digest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import sun.security.util.Password;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("administrator")
@CrossOrigin
@Slf4j
public class AdministratorController {
    @Autowired
    private AdministratorService administratorService;

    @RequestMapping("register")
    public Result register(@RequestBody Administrator administrator){
        Result result = new Result();
        try {
            administratorService.register(administrator);
            result.setStatus(1).setMsg("注册成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(0).setMsg(e.getMessage());
        }
        return result;
    }
    @RequestMapping("login")
    public Result login(@RequestBody Administrator administrator){
        Result result = new Result();
        try {
            Administrator admitDB = administratorService.login(administrator);
            result.setStatus(1).setMsg("登录成功");
            HashMap<String, String> map = new HashMap<>();
            map.put("id", String.valueOf(admitDB.getId()));
            map.put("name",admitDB.getName());
            String token = JWTUtil.getToken(map);
            result.setData(token);

        }catch (Exception e){
            result.setStatus(0).setMsg("用户名或密码错误");
        }
        return result;
    }
    @RequestMapping("getInfo")
    public Result getInfo(HttpServletRequest request){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        Administrator administrator = administratorService.getInfo(Integer.valueOf(id));
        result.setMsg("获取用户信息成功").setStatus(1).setData(administrator);
        return result;
    }
    @RequestMapping("update")
    public Result update(HttpServletRequest request,@RequestBody Administrator administrator){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        administrator.setId(Integer.valueOf(id));
        administratorService.update(administrator);
        result.setMsg("修改成功").setStatus(1);
        return result;
    }
    @RequestMapping("modifyPassword")
    public Result modifyPassword(HttpServletRequest request, @RequestBody Map<String,String>data){
        Result result = new Result();
        String password = DigestUtils.md5DigestAsHex(data.get("password").getBytes());
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        log.info("原密码：[{}],新密码：[{}]",data.get("password"),data.get("newPassword"));
        Administrator info = administratorService.getInfo(Integer.valueOf(id));
        if (!info.getPassword().equals(password)){
            result.setStatus(0).setMsg("原密码错误");
        }else{
            String newPassword = DigestUtils.md5DigestAsHex(data.get("newPassword").getBytes());
            administratorService.modifyPassword(Integer.valueOf(id),newPassword);
            result.setStatus(1).setMsg("修改成功");
           // String newPassword = DigestUtils.md5DigestAsHex(data.get("newPassword").getBytes());
           // log.info("加密后：[{}]",newPassword);
        }
        return result;
    }

}
